/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alluringdecors.model;

import com.alluringdecors.bean.Services;
import com.alluringdecors.helper.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Admin
 */
public class ServicesModel implements IAbstractModel<Services>{
    private static ServicesModel instance = null;
    Session session = null;
    private ServicesModel(){}
    public static ServicesModel getInstance() {
        if (instance == null) {
            instance = new ServicesModel();
        }
        return instance;
    }

    public List<Services> getListObjectById(int objID) {
        List<Services> lObject = null;
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Services where CategoryID = "+ objID + " order by ServicesID desc");
        lObject = q.list();
        //session.getTransaction().begin();
        return lObject;
    }

    public List<Services> getListObject() {
        List<Services> lObject = null;
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Services order by ServicesID desc");
        lObject = q.list();
        //session.getTransaction().begin();
        return lObject;
    }

    public List<Services> getListObjectTop4() {
        List<Services> lObject = null;
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Services order by ServicesID desc");
        q.setMaxResults(4);
        lObject = (List<Services>)q.list();
        //session.getTransaction().begin();
        return lObject;
    }

    public boolean insertObject(Services t) {
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            session.save(t);
            session.getTransaction().commit();
        }catch(Exception e) {
            System.out.println("Error UsersModel" + e.getMessage());
            session.getTransaction().rollback();
            return false;
        }
        return true;
    }

    public boolean deleteObject(int objID) {
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            session.delete(objID);
            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        return true;
    }

    public boolean updateObject(Services t) {
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            session.update(t);
            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        return true;
    }

    public List<Services> searchObject(String obj) {
        List<Services> lObject = null;
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Services where ServicesName like %" + obj + "%");
        lObject = q.list();
       // session.getTransaction().commit();
        return lObject;
    }

    public Services getServicesById(int id) {
        Services serv = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            Query query = session.createQuery("from Services where ServicesID='" + id +"'");
            serv = (Services) query.uniqueResult();
        }catch(Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
        return serv;
    }
}
